home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Snippets / New Venus / src / window.h < prev   
Encoding:
C/C++ Source or Header  |  1995-11-05  |  7.6 KB  |  197 lines  |  [TEXT/CWIE]

  1. /*
  2.  *************************************************************************
  3.  *
  4.  *                        A Basic Window class
  5.  *
  6.  *         A generic simple window that can either show up by itself
  7.  *    (and be dragged around on the screen and closed by clicking a
  8.  *                            "go-away" button)
  9.  *    or appear as a special item in a dialog
  10.  *
  11.  * Class ScreenWindow provides a very generic event and control processing
  12.  * that is common to regular windows with controls, modal and modeless
  13.  * dialogs. The basic functionality is to route events to appropriate
  14.  * handlers, process close/destroy/etc events, and handle controls.
  15.  * The class ScreenWindow provides only basic event handling, and it's
  16.  * abstract: you have to create your own class on the base of ScreenWindow
  17.  * and define the draw() function that is called on Update event to
  18.  * paint/repaint the window. You may want to redefine couple of other
  19.  * vanilla handlers (say, for control items (buttons, etc)).
  20.  *
  21.  * When you create the window using explicit specification of the rectangle, title, etc,
  22.  * the window would be invisible right after the creation. It's better to set up
  23.  * invisible (or hidden) attribute in the window resource if you create
  24.  * a window from the resource template. That way you still can do some
  25.  * rearrangements (say, create child windows) before the event processing
  26.  * starts.
  27.  *
  28.  * To show the window (with all the controls, children etc) and start
  29.  * processing of events, call show(). If you need to wait until the window
  30.  * is closed (after the user clicked some button or clicked at the go-away
  31.  * region, or selected "Close" from the menu, or something) call the
  32.  * serve_to_death() function. It returns only when all the opened windows
  33.  * are closed.
  34.  *
  35.  * $Id: Window.h,v 2.2 1994/11/07 20:33:30 oleg Exp oleg $
  36.  *
  37.  ***********************************************************************
  38.  */
  39.  
  40. #ifndef __GNUC__
  41. #pragma once
  42. #else
  43. #pragma interface
  44. #endif
  45.  
  46. #ifndef _Window_h
  47. #define _Window_h 1
  48.  
  49. /*
  50.  *----------------------------------------------------------------------
  51.  *                Service functions
  52.  */
  53.  
  54. class IMAGE;                // Opaque class
  55.  
  56. class ScreenRect : public Rect
  57. {
  58. public:
  59.   ScreenRect(const Rect& rect) : Rect(rect) {}
  60.   ScreenRect(const IMAGE& image);
  61. //  ScreenRect(const rowcol& heightwidth);
  62.                     // Create a rectangle of given height/width
  63.                     // positioned at a given point
  64. //  ScreenRect(const rowcol& origin, const rowcol& heightwidth);
  65.   ScreenRect& operator += (const int offset);
  66.   operator Rect * (void)                 { return this; }
  67.   int q_width(void)  const              { return right >= left ? right - left : left - right; }
  68.   int q_height(void) const              { return bottom >= top ? bottom - top : top - bottom; }
  69.                                 // Give a STATIC string representation of
  70.                                 // the rectangle
  71. //  operator const char * (void) const;
  72.   void print(const char * title = "") const;
  73. };
  74.  
  75.  
  76.                 // Make it easy setting and resetting
  77.                 // the current GrafPtr
  78. class SetNewGrafPtr
  79. {
  80.   GrafPtr old_port;
  81. public:
  82.                     // Switch to an off-screen grafworld
  83.   SetNewGrafPtr(const GrafPtr new_grafport) { GetPort(&old_port); SetPort(new_grafport); }
  84.                     // Restore the original grafworld
  85.  ~SetNewGrafPtr(void) { SetPort(old_port); }
  86. };
  87.  
  88. /*
  89.  *----------------------------------------------------------------------
  90.  *    A generic simple window that can be dragged around the screen
  91.  *        and closed by clicking a "go-away" button
  92.  * No other events are handled, redefine the event handler if necessary.
  93.  */
  94.  
  95. class ScreenWindow
  96. {
  97.   friend class ModelessDialog;
  98.   
  99.   WindowPtr this_window;
  100. //  Boolean Not_canceled;         // set to FALSE if Cancel button was pressed
  101.                                 // or the window was forcibly closed
  102.  
  103. //  static long universal_handler(WINDOW win, EVENT *ep);
  104. //  static int no_opened_windows;
  105.     
  106.   virtual Boolean handle_mouse_down(const EventRecord& the_event, WindowPtr where_window, short window_part);
  107.   virtual Boolean handle_key_down(const EventRecord& the_event);    // Handles key_down & auto_key events
  108.   void update(void);
  109.   virtual void draw(void) = 0;            // It is this function that really draws smth
  110.     
  111.   ScreenWindow(void) : this_window(nil) {}    // Constructor that does nothing, and initializes
  112.                                               // nothing. Trick to emulate a virtual constructor
  113. protected:
  114.   WindowPtr our_window(void) const     { return this_window; }
  115.  
  116.                                 // Draw w/o waiting for an UPDATE event:
  117.                                 // Use for animation
  118.   void draw_immediately(void)   { draw(); /*?update()?? */}
  119.  
  120.   virtual void destroy_it(void);            // I wouldn't've needed this is destructor had been
  121.                                               // virtual. Right now, CW 6.0 accepts attribute virtual
  122.                                               // for the destructor, but doesn't override
  123.                                               // destructors as it does for virtual functions.
  124.                                               // Oh, well, another kludge
  125.     
  126. public:
  127.   ScreenWindow(ScreenRect rect, const char * title);
  128.   ScreenWindow(const short resource_id);        // Create a window from a resource template
  129.   ~ScreenWindow(void)                    { destroy_it(); }
  130.   void refresh(void);
  131.                                 // Serve opened windows until all get closed
  132. //  static void serve_to_death(void);
  133.  
  134.                                 // Call this to start the service
  135.   void show(const Boolean onoff=TRUE) const  { ShowHide(this_window, onoff); }
  136. //  virtual void cancel(void);            // Close the window
  137.  // operator Boolean (void) const         { return Not_canceled; }
  138.   //Boolean is_this_window(const WindowPtr some_window) const { return some_window == this_window; }
  139.   //Boolean operator == (const WindowPtr some_window) const { return is_this_window(some_window); }
  140.  
  141.                                   // ScreenWindow event dispatch entry points
  142.                                   // Return FALSE if this object doesn't want any
  143.                                   // more events
  144.   virtual Boolean handle_null_event(const long event_time);
  145.   virtual Boolean handle_event(const EventRecord& the_event);
  146. };
  147.  
  148. /*
  149.  *----------------------------------------------------------------------
  150.  *                An off-screen pixel buffer for faster drawing
  151.  */
  152.  
  153. class OffScreenBuffer
  154. {
  155.   GWorldPtr graf_world;                // (Offscreen) graphical world that contains the picture
  156.  
  157. protected:
  158.   PixMapHandle pixmap;                // Pixmap for the offscreen world
  159.   int _height;                        // Dimensions of the pixmap (precomputed for
  160.   int _width;                        // easy reference
  161.   int _bytes_per_row;                // Note, bytes_per_row >= width (and generally, not equal)
  162.  
  163.                                       // Draw the offscreen buffer on screen
  164.   void draw(const Rect& where_rect, const Rect& from_rect);                // only part of it
  165.   void draw(const Rect& where_rect)        { draw(where_rect,(**pixmap).bounds); } // or the whole
  166.     
  167.   GrafPtr set_this_grafptr(void) const                // Set this grafptr and return the old one
  168.           { GrafPtr old_port; GetPort(&old_port); SetPort((GrafPtr)graf_world); return old_port; }
  169. public:
  170.   OffScreenBuffer(ScreenRect rect, const short clut_id=0);
  171.   ~OffScreenBuffer(void);
  172.  
  173.   PixMapHandle get_pixmap(void) const    { return pixmap; }
  174.         
  175.                                           // Bounding rect of the grafworld
  176.   ScreenRect q_bounds(void) const        { return (**pixmap).bounds; }
  177.   int height(void) const                { return _height; }         
  178.   int width(void) const                    { return _width; }         
  179.   int bytes_per_row(void) const            { return _bytes_per_row; }         
  180. };
  181.  
  182.                 // Make it easy setting and resetting
  183.                 // the current window
  184. class Set_NewGrafWorld
  185. {
  186.   CGrafPtr old_port;
  187.   GDHandle old_gdevice;
  188. public:
  189.                     // Switch to an off-screen grafworld
  190.   Set_NewGrafWorld(GWorldPtr graf_world)     
  191.     { GetGWorld(&old_port,&old_gdevice); SetGWorld((CGrafPtr)graf_world,nil); }
  192.                     // Restore the original grafworld
  193.  ~Set_NewGrafWorld(void) { SetGWorld(old_port,old_gdevice); }
  194. };
  195.  
  196. #endif
  197.